<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\goods\model;

use think\Model;

class GoodsAttributeModel extends Model
{

    public function adminGoodsAttrList($filter)
    {
        $where = [];

        $filter['goods_type'] = empty($filter['goods_type']) ? 0 : intval($filter['goods_type']);
        if( !empty($filter['goods_type']) ){
            $where['a.cat_id'] = $filter['goods_type'];
        }

        /* 查询-数据集 */
        $list = $this
             -> alias("a")
             -> field('a.*, t.cat_name')
             -> join('__GOODS_TYPE__ t', 'a.cat_id = t.cat_id', "LEFT")
             -> where($where)
             -> order("$filter[sort_by] $filter[list_order]")
             -> paginate(20);

        foreach ($list as $k => $v) {
            $lang_input_type                  = lang('VALUE_ATTR_INPUT_TYPE');
            $list[$k]['attr_input_type_desc'] = $lang_input_type[$v['attr_input_type']];
            $v['attr_values']                 = str_replace("\r\n", "\n", $v['attr_values']);
            $list[$k]['attr_values']          = str_replace("\n", ", ", $v['attr_values']);
        }

        return $list;
    }

}
